package com.administrator.cloudapplication.activitys;

import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.administrator.cloudapplication.Interface.NearItemClick;
import com.administrator.cloudapplication.R;
import com.administrator.cloudapplication.adapter.NearLibraryAdapter;
import com.administrator.cloudapplication.adapter.NewsListItemAdapter;
import com.administrator.cloudapplication.base.BaseActivity;
import com.administrator.cloudapplication.callback.ReqCallBack;
import com.administrator.cloudapplication.constant.Urls;
import com.administrator.cloudapplication.models.NearLibraryModel;
import com.administrator.cloudapplication.models.NewsListModel;
import com.administrator.cloudapplication.tools.GsonUtils;
import com.administrator.cloudapplication.tools.HttpUtils;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Administrator on 2017/4/26.
 */

public class NavigationActivity extends BaseActivity implements AdapterView.OnItemClickListener, PullToRefreshBase.OnRefreshListener2, ReqCallBack, NearItemClick {
    int type;
    int page = 1;
    EditText et_search;
    NearLibraryAdapter mNearLibraryAdapter;
    NewsListItemAdapter mNewsListItemAdapter;
    PullToRefreshListView mPullToRefreshListView;
    ArrayList<NearLibraryModel.DataBean> mLibraryModelDB = new ArrayList<>();
    ArrayList<NewsListModel.DataBean.NewsListBean> mNewsDataBean = new ArrayList<>();
    String url, cate;
    private boolean isDown=true;
    String keyword;
    @Override
    protected void initView() {
        setContentView(R.layout.activity_navigation);
        mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.ptr_listview_nav);
        et_search= (EditText) findViewById(R.id.et_search);
        et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    // 先隐藏键盘
                    ((InputMethodManager) et_search
                            .getContext()
                            .getSystemService(NavigationActivity.INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(NavigationActivity.this.getCurrentFocus()
                                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    if (et_search.getText().toString().isEmpty()) {
                        Toast.makeText(NavigationActivity.this,
                                "请输入关键字", Toast.LENGTH_SHORT).show();
                    } else {
                        mLibraryModelDB.clear();
                        mNewsDataBean.clear();
                        page = 1;
                        requestWeb();
                    }
                    return true;
                }
                return false;
            }
        });

        mPullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        mPullToRefreshListView.setOnItemClickListener(this);
        mPullToRefreshListView.setOnRefreshListener(this);
        initTitleView();
        type = getIntent().getIntExtra("type", type);
        switch (type) {
            case 0:
                tvTitle.setText("图书馆导航");
                url = Urls.LIBRARY_LIST;
                cate = "library";
                mNearLibraryAdapter = new NearLibraryAdapter(NavigationActivity.this, mLibraryModelDB, this);
                mPullToRefreshListView.setAdapter(mNearLibraryAdapter);
                break;
            case 1:
                tvTitle.setText("博物馆导航");
                url = Urls.LIBRARY_LIST;
                cate = "repository";
                mNearLibraryAdapter = new NearLibraryAdapter(NavigationActivity.this, mLibraryModelDB, this);
                mPullToRefreshListView.setAdapter(mNearLibraryAdapter);
                break;
            case 2:
                tvTitle.setText("文化政务");
                url = Urls.NEWS_LIST;
                cate = "PolicyText";
                mNewsListItemAdapter = new NewsListItemAdapter(NavigationActivity.this, mNewsDataBean);
                mPullToRefreshListView.setAdapter(mNewsListItemAdapter);
                break;
        }
    }

    @Override
    protected void initData() {
        requestWeb();
    }

    @Override
    protected void onLeftClick() {
        finish();
    }

    @Override
    protected void onRightClick() {

    }

    private void requestWeb() {
        showLoading();
        keyword= (et_search.getText()).toString();
        HashMap<String, String> param = new HashMap();
        param.put("curpage", page + "");
        param.put("cate", cate);
        param.put("keyword",keyword);
        HttpUtils.getInstance(this).requestPostByAsyn(url, param, this, 0);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        isDown=true;
        page = 1;
        requestWeb();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        isDown=false;
        page++;
        requestWeb();
    }

    @Override
    public void onReqSuccess(String result, int flag) {
        stopLoading();
        if (mPullToRefreshListView.isRefreshing()) {
            mPullToRefreshListView.onRefreshComplete();
        }
        if (isDown) {
            mNewsDataBean.clear();
            mLibraryModelDB.clear();
            page = 1;
        }
        try {
            JSONObject jsonObject = new JSONObject(result);
            JSONArray array = jsonObject.getJSONArray("data");
            Object object = array.get(0);
            if (null == object || "null".equals(object.toString())) {
                showToast("没有更多了");
                return;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        switch (type) {
            case 0:
            case 1:
                NearLibraryModel mLibraryModel = GsonUtils.json2Bean(result, NearLibraryModel.class);
                mLibraryModelDB.addAll(mLibraryModel.getData());
                mNearLibraryAdapter.notifyDataSetChanged();
                break;
            case 2:
                NewsListModel mNewsListModel = GsonUtils.json2Bean(result, NewsListModel.class);
                mNewsDataBean.addAll(mNewsListModel.getData().getNews_list());
                mNewsListItemAdapter.notifyDataSetChanged();
                break;
        }

    }

    @Override
    public void onReqFailed(String errorMsg, int flag) {
        Toast.makeText(this, "请检查您的网络", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(NearLibraryModel.DataBean mDataBean) {

    }
}
